package com.example.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.pojo.entity.Manager;
import com.example.properties.JwtProperties;
import com.example.result.Result;
import com.example.service.IManagerService;
import com.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjw
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    private IManagerService iManagerService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping("/login")
    public Result handleSignIn(@RequestBody Manager manager) {
       try{
           LambdaQueryWrapper<Manager> lambdaQueryWrapper = new LambdaQueryWrapper<>();
           lambdaQueryWrapper.eq(Manager::getName, manager.getName());
           Manager manager1 =iManagerService.getOne(lambdaQueryWrapper);
           Map<String, Object> claims = new HashMap<>();
           claims.put("managerId", manager1.getId());
           String  token = JwtUtil.createJWT(
                   jwtProperties.getAdminSecretKey(),
                   jwtProperties.getAdminTtl(),
                   claims);
           return Result.success(token);
       } catch (Exception e) {
           return Result.error(e);
       }
    }

}
